/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package control_escolar;

import static control_escolar.inicio.constancia_inicio;
import static control_escolar.log.claves_sesion2;
import static control_escolar.log.expedientes;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author danielsaid
 */
public class Maestro_calificaciones extends javax.swing.JFrame {

    /**
     * Creates new form Maestro
     */
    public Maestro_calificaciones() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
         public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("umbrella-corp-2-icon.png"));
        return retValue;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        agregar_calificaciones = new javax.swing.JButton();
        regresar_mcalif = new javax.swing.JButton();
        No_expediente = new javax.swing.JTextField();
        nombre_alumno = new javax.swing.JTextField();
        calificacion = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        clave_materia = new javax.swing.JTextField();
        parcial1 = new javax.swing.JTextField();
        parcial2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        parcial3 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        parcial4 = new javax.swing.JTextField();
        nombre_materia = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Alta de Calificaciones");
        setIconImage(getIconImage());
        setResizable(false);

        jLabel1.setText("No. Expediente:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Materia:");

        jLabel4.setText("Parcial 1:");

        jLabel5.setText("Calificacion:");

        agregar_calificaciones.setText("Agregar");
        agregar_calificaciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregar_calificacionesActionPerformed(evt);
            }
        });

        regresar_mcalif.setText("Regresar");
        regresar_mcalif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regresar_mcalifActionPerformed(evt);
            }
        });

        No_expediente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                No_expedienteKeyTyped(evt);
            }
        });

        calificacion.setEditable(false);
        calificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calificacionActionPerformed(evt);
            }
        });
        calificacion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                calificacionKeyTyped(evt);
            }
        });

        jLabel6.setText("Parcial 2:");

        jLabel7.setText("Clave de la Materia");

        parcial1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                parcial1FocusLost(evt);
            }
        });
        parcial1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                parcial1KeyTyped(evt);
            }
        });

        parcial2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parcial2ActionPerformed(evt);
            }
        });
        parcial2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                parcial2KeyTyped(evt);
            }
        });

        jLabel8.setText("Parcial 3:");

        parcial3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                parcial3KeyTyped(evt);
            }
        });

        jLabel9.setText("Parcial 4:");

        parcial4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                parcial4KeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(agregar_calificaciones)
                        .addGap(51, 51, 51)
                        .addComponent(regresar_mcalif)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel6)
                                                .addComponent(jLabel8)
                                                .addComponent(jLabel9))
                                            .addGap(24, 24, 24))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addGap(19, 19, 19)))
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(parcial4, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                                    .addComponent(parcial3)
                                    .addComponent(parcial2)
                                    .addComponent(calificacion)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(No_expediente)
                                    .addComponent(nombre_alumno)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(clave_materia)
                                    .addComponent(nombre_materia)
                                    .addComponent(parcial1))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(No_expediente))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombre_alumno))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(clave_materia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombre_materia)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parcial1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parcial2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parcial3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parcial4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(calificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regresar_mcalif)
                    .addComponent(agregar_calificaciones))
                .addContainerGap())
        );

        jLabel1.getAccessibleContext().setAccessibleName("numcontrol");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void regresar_mcalifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regresar_mcalifActionPerformed
        // TODO add your handling code here:
      validando();
    }//GEN-LAST:event_regresar_mcalifActionPerformed

    private void agregar_calificacionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregar_calificacionesActionPerformed
        try {
            // TODO add your handling code here
            agregarcalificaciones();
            JOptionPane.showMessageDialog(null, "Se Agrego Correctamente");
        } catch (IOException ex) {
            Logger.getLogger(Maestro_calificaciones.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_agregar_calificacionesActionPerformed

    private void No_expedienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_No_expedienteKeyTyped
        // TODO add your handling code here:
        char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_No_expedienteKeyTyped

    private void calificacionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_calificacionKeyTyped
        // TODO add your handling code here:
        char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_calificacionKeyTyped

    private void calificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calificacionActionPerformed
        // TODO add your handling code here:
      
    }//GEN-LAST:event_calificacionActionPerformed

    private void parcial2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parcial2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_parcial2ActionPerformed

    private void parcial1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parcial1KeyTyped
        // TODO add your handling code here:
         char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_parcial1KeyTyped

    private void parcial2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parcial2KeyTyped
        // TODO add your handling code here:
         char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_parcial2KeyTyped

    private void parcial3KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parcial3KeyTyped
        // TODO add your handling code here:
         char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_parcial3KeyTyped

    private void parcial4KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parcial4KeyTyped
        // TODO add your handling code here:
         char c= evt.getKeyChar();

        if (Character.isLetter(c)){
        evt.consume();
        }
    }//GEN-LAST:event_parcial4KeyTyped

    private void parcial1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_parcial1FocusLost
        // TODO add your handling code here:
        
    }//GEN-LAST:event_parcial1FocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Maestro_calificaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Maestro_calificaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Maestro_calificaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Maestro_calificaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Maestro_calificaciones().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField No_expediente;
    private javax.swing.JButton agregar_calificaciones;
    public static javax.swing.JTextField calificacion;
    private javax.swing.JTextField clave_materia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField nombre_alumno;
    private javax.swing.JTextField nombre_materia;
    private javax.swing.JTextField parcial1;
    private javax.swing.JTextField parcial2;
    private javax.swing.JTextField parcial3;
    private javax.swing.JTextField parcial4;
    private javax.swing.JButton regresar_mcalif;
    // End of variables declaration//GEN-END:variables

    public int agregarcalificaciones()throws IOException {
        String Curso;
        int A = Integer.parseInt(parcial1.getText());
        int B = Integer.parseInt(parcial2.getText());
        int C = Integer.parseInt(parcial3.getText());
        int D = Integer.parseInt(parcial4.getText());
        int Cal = 0;
        Cal = Cal+A+B+C+D;
        Cal =  (Cal)/(4);
        if(Cal > 70){
        Curso = "Ordinario";
        }else{
        Curso = "especial";    
        }
        
        String Final = Integer.toString(Cal);        
        PrintWriter printwriter = new PrintWriter(new FileWriter("src/ArchivosTXT/Calificaciones.txt", true));
        printwriter.print(No_expediente.getText() + ";");
        printwriter.print(clave_materia.getText() + ";");
        printwriter.print(nombre_materia.getText() + ";");
        printwriter.print(parcial1.getText() + ";");
        printwriter.print(parcial2.getText() + ";");
        printwriter.print(parcial3.getText() + ";");
        printwriter.print(parcial4.getText() + ";");
        printwriter.print(Final + ";");
        printwriter.println(Curso + ";");
        //printwriter.print(nombre_alumno.getText() + ";");
        printwriter.flush(); 
        No_expediente.setText(""); 
        nombre_alumno.setText("");
        clave_materia.setText("");               
        nombre_materia.setText("");
        parcial1.setText("");
        parcial2.setText("");
        parcial3.setText("");
        parcial4.setText("");
        calificacion.setText(Final);
        return Cal;
    }
   public void validando() {
        String EXP = log.expedientes.getText();
        String pwd = log.claves_sesion2.getText();
        if (expedientes.getText().length() != 0 && claves_sesion2.getText().length() != 0) {
            try {

                BufferedReader alumnos = new BufferedReader(new FileReader("src/ArchivosTXT/Alumnos.txt"));
                String Materias;
                while ((Materias = alumnos.readLine()) != null) {

                    if (Materias.startsWith(EXP)) {
                        String[] Al = Materias.split(";");
                        if (Al[0].equals(EXP) && Al[22].equals(pwd)) {
                            
                            new inicio().setVisible(true);
                            this.setVisible(false);
                            inicio.constancia_inicio.setVisible(false);
                            inicio.reporte1.setVisible(false);
                            inicio.reporte2.setVisible(true);
                            inicio.reporte3.setVisible(false);
                            inicio.kardex_inicio.setVisible(true);
                            inicio.jMenu2.setVisible(false);
                        } else {
                            JOptionPane.showMessageDialog(null, "Datos Erroneos");
                        }
                    }
                }

                BufferedReader maestros = new BufferedReader(new FileReader("src/ArchivosTXT/Maestros.txt"));
                String Maestros;
                while ((Maestros = maestros.readLine()) != null) {
                    if (Maestros.startsWith(EXP)) {
                        String[] Ma = Maestros.split(";");
                        if (Ma[0].equals(EXP) && Ma[12].equals(pwd)) {
                            
                            new inicio().setVisible(true);
                            this.setVisible(false);
                            inicio.constancia_inicio.setVisible(false);
                            inicio.reporte1.setVisible(false);
                            inicio.reporte2.setVisible(true);
                            inicio.reporte3.setVisible(false);
                            inicio.kardex_inicio.setVisible(false);
                            inicio.jMenu2.setVisible(true);
                            inicio.alumno_inicio.setVisible(false);
                            inicio.maestro_inicio.setVisible(false);
                            inicio.materia_inicio.setVisible(false);
                            inicio.Calificaciones.setVisible(true);
                            inicio.jMenuItem6.setVisible(false);
                            //inicio.jMenuItem7.setVisible(false);

                        } else {
                            JOptionPane.showMessageDialog(null, "Datos Erroneos");
                        }
                    }
                }
                if (EXP.equals("Admin") && pwd.equals("Citrus")) {
                    
                    new inicio().setVisible(true);
                    this.setVisible(false);
                    constancia_inicio.setVisible(true);
                    inicio.reporte1.setVisible(true);
                    inicio.reporte2.setVisible(true);
                    inicio.reporte3.setVisible(true);
                    inicio.kardex_inicio.setVisible(true);
                    inicio.jMenu2.setVisible(true);
                    inicio.alumno_inicio.setVisible(true);
                    inicio.maestro_inicio.setVisible(true);
                    inicio.materia_inicio.setVisible(true);
                    inicio.Calificaciones.setVisible(true);
                    inicio.jMenuItem4.setVisible(true);
                    inicio.jMenuItem6.setVisible(true);
                    inicio.jMenuItem7.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Datos Erroneos");
                }
            } catch (IOException x) {
            }
        } else {
            JOptionPane.showMessageDialog(null, "Campos Requeridos");
        }
}

}
